package com.yuyou.fn.controller;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.github.wens.mybatisplus.plugins.Page;
import com.yuyou.fn.common.constant.Constants;
import com.yuyou.fn.common.util.Utils;
import com.yuyou.fn.educational.entity.*;
import com.yuyou.fn.educational.service.*;
import com.yuyou.fn.educational.vo.*;
import com.yuyou.fn.platform.entity.SysUser;
import com.yuyou.fn.platform.service.ISysUserService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.List;

/**
 * Created by wens on 16-11-29.
 */
@Controller
@RequestMapping("/api/admin")
public class AdminCourseApiController extends AdminApiBaseController {

    @Resource
    private ISysUserService sysUserService;

    @Resource
    private ICourseTemplateService courseTemplateService;

    @Resource
    private ICourseClassService courseClassService;

    @Resource
    private ICourseTypeService courseTypeService;

    @Resource
    private IProductService productService;

    @Resource
    private IClassTimeService classTimeService ;

    @Resource
    private IStudentRegService studentRegService;



    @ApiOperation("课程列表")
    @ApiImplicitParams({@ApiImplicitParam(value = "className", required = false), @ApiImplicitParam(value = "gradeName", required = false), @ApiImplicitParam(value = "subjectName", required = false), @ApiImplicitParam(value = "teacherNames", required = false), @ApiImplicitParam(value = "seniorName", required = false), @ApiImplicitParam(value = "roomName", required = false), @ApiImplicitParam(value = "periodName", required = false), @ApiImplicitParam(value = "progressStatus", required = false), @ApiImplicitParam(value = "campusName", required = false), @ApiImplicitParam(value = "openStatus", required = false), @ApiImplicitParam(value = "areaTeamName", required = false), @ApiImplicitParam(value = "busTeamName", required = false), @ApiImplicitParam(value = "areaTeamId", required = false), @ApiImplicitParam(value = "busTeamId", required = false)})
    @RequestMapping(value = "/courseTemplateList", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Page<CourseTemplate>> courseTemplateList(CourseTemplateQueryVo courseTemplateQuery, @RequestParam(value = "pageNo", defaultValue = "1") int pageNo, @RequestParam(value = "pageSize", defaultValue = "20") int pageSize, HttpServletRequest request) {
        SysUser user = getUser(request);
        if (user.getUserType() == Constants.USER_TYPE_OF_AREA) {
            courseTemplateQuery.setAreaTeamId(getUser(request).getAreaTeamId());
        } else if (user.getUserType() == Constants.USER_TYPE_OF_BUS) {
            courseTemplateQuery.setBusTeamId(user.getBusTeamId());
        }
        Page<CourseTemplate> page = courseTemplateService.findCourseTemplateByQueryForPage(courseTemplateQuery, pageNo, pageSize);
        return ReturnResult.success(page);
    }

    @ApiOperation("课程详情")
    @RequestMapping(value = "/courseTemplateDetail", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<CourseTemplate> courseTemplateDetail(@RequestParam("courseTemplateId") Long courseTemplateId) {
        CourseTemplate courseTemplate = courseTemplateService.findById(courseTemplateId);
        return ReturnResult.success(courseTemplate);
    }

    @ApiOperation("产品详情")
    @RequestMapping(value = "/productDetail", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Product> productDetail(@RequestParam("productId") Long productId) {
        Product product = productService.findById(productId);
        return ReturnResult.success(product);
    }

    @ApiOperation("保存更新课程")
    @RequestMapping(value = "/saveOrUpdateCourseTemplate", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> saveOrUpdateCourseTemplate(CourseTemplateSaveOrUpdateVo courseTemplateSaveOrUpdateVo) {
        courseTemplateService.saveOrUpdateCourseTemplateList(courseTemplateSaveOrUpdateVo);
        return ReturnResult.success("Ok");
    }

    @ApiOperation("删除课程")
    @RequestMapping(value = "/deleteCourseTemplate", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> deleteCourseTemplate(@RequestParam("courseTemplateId") Long courseTemplateId ) {
        courseTemplateService.deleteCourse(courseTemplateId);
        return ReturnResult.success("Ok");
    }

    @ApiOperation("保存更新产品")
    @RequestMapping(value = "/saveOrUpdateProduct", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> saveOrUpdateProduct(ProductSaveOrUpdateVo productSaveOrUpdateVo) {
        productService.saveOrUpdateProduct(productSaveOrUpdateVo);
        return ReturnResult.success("Ok");
    }

    @ApiOperation("更改课程")
    @RequestMapping(value = "/changeCourseStatus", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> changeCourseStatus(@RequestParam("courseTemplateId") Long courseTemplateId, @RequestParam("status") Integer status) {
        courseTemplateService.changeCourseStatus(courseTemplateId, status);
        return ReturnResult.success("Ok");
    }

    @ApiOperation("课程类型列表")
    @RequestMapping(value = "/courseTypeList", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<List<CourseType>> courseTypeList(HttpServletRequest request) {

        List<CourseType> courseTypeList = courseTypeService.findAllCourseTypeFromCache();

        return ReturnResult.success(courseTypeList);
    }

    @ApiOperation("产品列表")
    @RequestMapping(value = "/productList", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<List<Product>> productList(ProductQueryVo productQueryVo, @RequestParam(value = "pageNo", defaultValue = "1") int pageNo, @RequestParam(value = "pageSize", defaultValue = "20") int pageSize, HttpServletRequest request) {
        Page<Product> page = productService.findProductByQueryForPage(productQueryVo, pageNo, pageSize);
        return ReturnResult.success(page);
    }

    @ApiOperation("更改产品")
    @RequestMapping(value = "/changeProductStatus", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> changeProductStatus(@RequestParam("productId") Long productId, @RequestParam("status") Integer status) {
        productService.changeProductStatus(productId, status);
        return ReturnResult.success("Ok");
    }

    @ApiOperation("删除产品")
    @RequestMapping(value = "/deleteProduct", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> deleteProduct(@RequestParam("productId") Long productId) {
        productService.deleteProduct(productId);
        return ReturnResult.success("Ok");
    }

    @ApiOperation("班级列表")
    @RequestMapping(value = "/courseClassList", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Page<CourseClass>> courseClassList(CourseClassQueryVo courseClassQueryVo, @RequestParam(value = "pageNo", defaultValue = "1") int pageNo, @RequestParam(value = "pageSize", defaultValue = "20") int pageSize, HttpServletRequest request) {

        SysUser user = getUser(request);
        courseClassQueryVo.setAreaTeamId(user.getAreaTeamId());
        courseClassQueryVo.setBusTeamId(user.getBusTeamId());
        Page<CourseClass> page = courseClassService.findCourseClassByQueryForPage(courseClassQueryVo, pageNo, pageSize);
        return ReturnResult.success(page);
    }

    @ApiOperation("班级详情")
    @RequestMapping(value = "/courseClassDetail", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<CourseClass> courseClassDetail(@RequestParam("courseClassId") Long courseClassId) {
        CourseClass courseClass = courseClassService.findById(courseClassId);
        CourseTemplate courseTemplate = courseTemplateService.findById(courseClass.getCourseTemplateId());
        ClassCourseVo classCourseVo = new ClassCourseVo();
        classCourseVo.setCourseClass(courseClass);
        classCourseVo.setCourseTemplate(courseTemplate);
        return ReturnResult.success(classCourseVo);
    }

    @ApiOperation("保存更新班级")
    @RequestMapping(value = "/saveOrUpdateCourseClass", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> saveOrUpdateCourseClass(CourseClassSaveOrUpdateVo courseClassSaveOrUpdateVo) {
        courseClassService.saveOrUpdateCourseClass(courseClassSaveOrUpdateVo);
        return ReturnResult.success("Ok");
    }

    @ApiOperation("保存更新班级")
    @RequestMapping(value = "/changeCourseClassStatus", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> changeCourseClassStatus(@RequestParam("status") Integer status , @RequestParam("classIds") String classIdsStr) {
        courseClassService.changeStatus(Utils.toLongIds(classIdsStr),status);
        return ReturnResult.success("Ok");
    }

    @ApiOperation("获取基础产品列表")
    @RequestMapping(value = "/baseProductList", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<List<Product>> baseProductList(HttpServletRequest request) {
        SysUser user = getUser(request);
        List<Product> list = productService.findBaseProductForList(user.getAreaTeamId());
        return ReturnResult.success(list);
    }

    @ApiOperation("获取基础课程列表")
    @RequestMapping(value = "/baseCourseList", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<List<CourseTemplate>> baseCourseList(HttpServletRequest request) {
        SysUser user = getUser(request);

        List<CourseTemplate> list = courseTemplateService.findBaseCourseForList(user.getAreaTeamId());
        return ReturnResult.success(list);
    }

    @ApiOperation("导入课程Excel")
    @RequestMapping(value = "/courseTemplateImport", method = RequestMethod.POST)
    @ResponseBody
    public ReturnResult<Void> courseTemplateImport(HttpServletRequest request, MultipartFile file) throws Exception {
        SysUser user = getUser(request);
        InputStream inputStream = file.getInputStream();
        ImportParams params = new ImportParams();
        List<CourseTemplate> courseTemplateList = ExcelImportUtil.importExcel(inputStream, CourseTemplate.class, params);
        courseTemplateService.saveOrUpdateCourseTemplateList(courseTemplateList, user);
        return ReturnResult.success("ok");
    }

    @ApiOperation("保存排时间结果")
    @RequestMapping(value = "/saveArrangeClassTimeResult", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> saveArrangeClassTimeResult(@RequestParam("classId") Long classId , @RequestParam("resultJsonStr") String resultJsonStr) {

        List<ArrangeClassTimeResultVo> arrangeClassTimeResultList = JSONObject.parseObject(resultJsonStr, new TypeReference<List<ArrangeClassTimeResultVo>>() {
        });
        courseClassService.saveArrangeClassTimeResult(classId, arrangeClassTimeResultList );
        return ReturnResult.success("Ok");
    }

    @ApiOperation("推荐班级")
    @RequestMapping(value = "/recommendCourseClass", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> recommendCourseClass(@RequestParam("classIds[]") List<Long> classIds , @RequestParam("isRecommend") Integer isRecommend ) {
        courseClassService.recommendCourseClass(classIds,isRecommend);
        return ReturnResult.success("Ok");
    }



}